package com.sx.micowell.framework.model.smartDevice;

import android.widget.Toast;

import com.example.mvvm.base.data.MutableLiveListData;
import com.example.mvvm.base.data.SingleLiveEvent;
import com.example.mvvm.base.vm.BaseViewModel;
import com.example.mvvm.rx.RxBus;
import com.example.mvvm.until.LogUtils;
import com.example.mvvm.until.SPUtils;
import com.sx.micowell.framework.app.AppConstant;
import com.sx.micowell.framework.app.SPKey;
import com.sx.micowell.framework.tools.EventInfo;
import com.tuya.smart.android.ble.builder.BleConnectBuilder;
import com.tuya.smart.home.sdk.TuyaHomeSdk;
import com.tuya.smart.home.sdk.api.ITuyaHomeChangeListener;
import com.tuya.smart.home.sdk.bean.HomeBean;
import com.tuya.smart.home.sdk.callback.ITuyaGetHomeListCallback;
import com.tuya.smart.home.sdk.callback.ITuyaHomeResultCallback;
import com.tuya.smart.sdk.api.IDevListener;
import com.tuya.smart.sdk.bean.DeviceBean;


import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * @Author: YY
 * @Time: 2021/9/16
 * @Describe TODO
 */
public class SmartDeviceListModel extends BaseViewModel {

    public MutableLiveListData<DeviceBean> deviceListData = new MutableLiveListData<>();

    public SingleLiveEvent<HomeBean> homeBeanEvent = new SingleLiveEvent<>();

    @Override
    public void registerRxBus() {
        super.registerRxBus();
        addSubscribe(RxBus.getDefault().toObservable(EventInfo.class)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(eventInfo -> {
                    if (eventInfo.getType() == AppConstant.EVENT_DEVICE_BIND_SUCCESS) {
                        queryHomeList();
                    }
                })
        );
    }

    /**
     * 查询家庭列表
     */
    public void queryHomeList() {
        TuyaHomeSdk.getHomeManagerInstance().queryHomeList(new ITuyaGetHomeListCallback() {
            @Override
            public void onSuccess(List<HomeBean> list) {
                if (!list.isEmpty()) {
                    LogUtils.d("setCurrentFamily " +list.get(0).getHomeId());
                    SPUtils.getInstance().put(SPKey.SP_DEVICE_HOME_ID, list.get(0).getHomeId());
                    TuyaHomeSdk.newHomeInstance(list.get(0).getHomeId());
                    getBoundDeviceList(list.get(0).getHomeId());
                } else {
                    LogUtils.d("home list is null,plz create home");
                    requestCreateHome();
                }
            }

            @Override
            public void onError(String s, String s1) {
                LogUtils.d("queryHomeList onError " + s + "\n" + s1);
            }
        });
    }

    /**
     * 创建默认家庭组
     */
    public void requestCreateHome() {
        TuyaHomeSdk.getHomeManagerInstance().createHome(
                "我的家...", 0, 0, "", new ArrayList<>(),

                new ITuyaHomeResultCallback() {
                    @Override
                    public void onSuccess(HomeBean bean) {
                        LogUtils.d("Create Home success");
                        queryHomeList();
                    }

                    @Override
                    public void onError(String errorCode, String errorMsg) {
                        LogUtils.d("Create Home error->$errorMsg");
                    }
                }
        );
    }

    /**
     * 获取设备列表
     */
    public void getBoundDeviceList(long homeId) {

        TuyaHomeSdk.newHomeInstance(homeId).getHomeDetail(new ITuyaHomeResultCallback() {
            @Override
            public void onSuccess(HomeBean bean) {
                LogUtils.d("getHomeDetail onSuccess "+bean.getDeviceList().size());
                deviceListData.setValue(bean.getDeviceList());
                homeBeanEvent.setValue(bean);

            }

            @Override
            public void onError(String errorCode, String errorMsg) {
                LogUtils.d("getHomeDetail onError");

            }
        });
    }
}
